package com.dp.util;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
public class LanguageResources {

	private  Properties languageProperties = null;
	private static LanguageResources resourcesSetting = null;
	private String  languageType = "zh-cn";
	
	private LanguageResources(HttpServletRequest request)
	{
		String language=request.getHeader("accept-language");
		language=language==null?languageType:language;
		languageType=language;
		
		language=language==null?languageType:language.split(",")[0];
		if("zh-cn".equalsIgnoreCase(language))
			language="MessageResources_zh.properties";		
		else
			language="MessageResources_en.properties";
		
		languageProperties = new MessagePropertise().LoadPropertie(language);
		resourcesSetting=this;
	}
	public static LanguageResources getResourcesSetting(HttpServletRequest request)
	{
		String acceptLanguage=request.getHeader("accept-language");
		if(resourcesSetting==null||!resourcesSetting.getLanguageType().equals(acceptLanguage))
		{
			return new LanguageResources(request);
		}
		return resourcesSetting;
	}
	public Properties getLanguageProperties() {
		return languageProperties;
	}
	public  String get(String key ,String defaultValue)
	{
		return languageProperties.getProperty(key, defaultValue);
	}
	public  String get(String key)
	{
		return get(key, key);
	}
	public String getLanguageType() {
		return languageType;
	}
	public void setLanguageType(String languageType) {
		this.languageType = languageType;
	}
	
}
